/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practice.de1;

import demo.file.TextFile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ManagerEvent {

    private final String fileName = "src\\practice\\de1\\Event.txt";
    private ArrayList<Event> eventList;

    public ManagerEvent() {
        this.eventList = new ArrayList<>();
        readFromTextFile();
    }

    public void createEvent() {
        Scanner keyboard = new Scanner(System.in);

        Event e = new Event();

        System.out.printf("Time : ");
        e.setTime(keyboard.nextLine());
        System.out.printf("Place : ");
        e.setPlace(keyboard.nextLine());
        System.out.printf("Content : ");
        e.setContent(keyboard.nextLine());

        eventList.add(e);
        writeToTextFile(e);
    }

    private void writeToTextFile(Event e) {
        BufferedWriter pw = null;
        try {
            pw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(fileName, true),
                    "UTF-8"));

            pw.write(e.getTime() + "-" + e.getPlace() + "-" + e.getContent());
            pw.newLine();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(TextFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ManagerEvent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEvent.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pw.close();
            } catch (IOException ex) {
                Logger.getLogger(ManagerEvent.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    private void readFromTextFile() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(
                    new InputStreamReader(
                    new FileInputStream(fileName)));
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("-");

                if (parts.length == 3) {
                    this.eventList.add(
                            new Event(parts[0], parts[1], parts[2]));
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ManagerEvent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEvent.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(ManagerEvent.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void findEvent() {
        Scanner keyboard = new Scanner(System.in);

        System.out.printf("Enter place : ");
        String place = keyboard.nextLine();

        for (Event e : eventList) {
            if (e.getPlace().equals(place)) {
                e.display();
                break;
            }
        }

    }

    public void listEvent() {
        for (Event e : eventList) {
            e.display();
        }
    }
}
